#include "fileOperationsHeader.h"
char* copyFile(char spath[], char dpath[])
{
	 if(mh==NULL)
		 return ERR_VFS_COPYFILE_07;
     if(spath==NULL||spath[0]!='/')
         return ERR_VFS_COPYFILE_01;
     if(dpath==NULL||dpath[0]!='/')
         return ERR_VFS_COPYFILE_02;
     int z=strlen(spath);
	 if(z!=1 && spath[z-1]=='/')
		 spath[z-1]='\0';
	 z=strlen(dpath);
	 if(z!=1 && dpath[z-1]=='/')
		 dpath[z-1]='\0';
	 FILE *fp=fopen(mh->fileSystemLabel,"r+b");
     int currentFileDescriptors=mh->countOfFileDescriptors;
     int maxFileDescriptors=mh->maxDescriptors;
     if(currentFileDescriptors==maxFileDescriptors)
		 return ERR_VFS_COPYFILE_06;
     int noOfBlocks=maxFileDescriptors/2;
     int j=0;
     descriptors *s=(descriptors*)malloc(sizeof(descriptors));
     s=(descriptors*)searchBST(spath);
     int i=strlen(spath)-1;
	 for(;spath[i]!='/';i--);
	 char *srcParent=(char*)malloc(sizeof(char)*(i+1));
	 j=0;
	 for(;j<i;j++)
		 srcParent[j]=spath[j];
	 srcParent[j]='\0';
	 char *src=(char*)malloc(sizeof(char)*(strlen(spath)-1-i+1));
	 j=0;
	 i++;
	 for(;spath[i]!='\0';j++)
	 {
		 src[j]=spath[i];
		 i++;
	 }
	 src[j]='\0';
	 if(strcmp(srcParent,"")==0 || strcmp(srcParent," ")==0)
		 srcParent="/";
     int iterDescriptors=0;
     if(s==NULL)
     {
         while(iterDescriptors<maxFileDescriptors)
         {
             if(strcmp(h->desc[iterDescriptors].fileName,src)==0 && strcmp(h->desc[iterDescriptors].location,srcParent)==0 && h->desc[iterDescriptors].type=='d')
                 return ERR_VFS_COPYFILE_05;
             iterDescriptors++;
         }
         return ERR_VFS_COPYFILE_01;
     }
     descriptors *d=(descriptors*)malloc(sizeof(descriptors));
     d=(descriptors*)searchBST(dpath);
     if(d==NULL)
		 return ERR_VFS_COPYFILE_02;
     int destPathLength=strlen(dpath)-1;
	 for(;dpath[destPathLength]!='/';destPathLength--);
	 char *destParent=(char*)malloc(sizeof(char)*(destPathLength+1));
	 j=0;
	 for(;j<destPathLength;j++)
		 destParent[j]=dpath[j];
	 destParent[j]='\0';
	 if(strcmp(destParent,"")==0 || strcmp(destParent," ")==0)
		 destParent="/";
	 char *dest=(char*)malloc(sizeof(char)*(strlen(dpath)-1-destPathLength+1));
	 j=0;
	 destPathLength++;
	 for(;dpath[destPathLength]!='\0';j++)
	 {
		 dest[j]=dpath[destPathLength];
		 destPathLength++;
	 }
	 dest[j]='\0';
	 iterDescriptors=0;
     for(i=0;i < noOfBlocks;i++)
     {
         if(h->freeList[i]=='0')
             break;
     }
     sprintf(d->fileName,"%s",dest);
     sprintf(d->location,"%s",destParent);
     d->size=s->size;
     char *str=(char*)malloc(sizeof(char)*s->size);
     fseek(fp,(int)arr[s->blockNumber],SEEK_SET);
     fread(str,sizeof(char)*s->size,1,fp);
     fseek(fp,(int)arr[d->blockNumber],SEEK_SET);
     fwrite(str,sizeof(char)*s->size,1,fp);
     for(i=0;i < maxFileDescriptors;i++)
     {
         if(h->desc[i].type=='f' && strcmp(h->desc[i].fileName,d->fileName)==0 && strcmp(h->desc[i].location,d->location)==0 && h->desc[i].blockNumber==d->blockNumber)
             break;
     }
     h->desc[i]=*d;
     mh->countOfFileDescriptors=mh->countOfFileDescriptors+1;
     char *absolutePathForBST=NULL;
	 absolutePathForBST=(char*)malloc(sizeof(char)*(strlen(h->desc[i].location)+1+strlen(h->desc[i].fileName)+1));
	 if(strcmp(h->desc[i].location,"/")!=0)
		 sprintf(absolutePathForBST,"%s/%s",h->desc[i].location,h->desc[i].fileName);
	 else
		 sprintf(absolutePathForBST,"%s%s",h->desc[i].location,h->desc[i].fileName);
     int bstRet=insertBST(absolutePathForBST,h->desc[i].blockNumber);
	 if(bstRet!=0)
		 return "FAILURE <while inserting into BST>";
	 int nAryRet=createnAryTree(maxFileDescriptors,h->desc);
	 if(nAryRet!=0)
		 return "FAILURE <while creating nAryTree>";
	 node *hashTableInsertionReturn=insert_ht(hashtable,h->desc[i].fileName,h->desc[i].blockNumber,h->desc[i].location);
	 if(hashTableInsertionReturn==NULL)
		 return "FAILURE <while entering into hashTable>";
	 //fclose(fp);
     return "SUCCESS";
}
